package com.example.ysdt.controller;

import com.example.ysdt.Vo.CategoryVo;
import com.example.ysdt.Vo.RecommendVo;
import com.example.ysdt.entiey.Category;
import com.example.ysdt.entiey.Recommend;
import com.example.ysdt.service.RecommendService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/Recommend")
@Api(value = "推荐接口", tags = {"推荐接口"})
public class RecommendController {
    @Autowired
    RecommendService recommendService;

    @PostMapping("queryLabel")
    @ApiOperation("查询标签")
    public Map<String,Object> queryLabel(@RequestBody RecommendVo recommendVo){
        Map<String, Object> Result = new HashMap<>();
        try {
            List<Recommend> labels = recommendService.queryLabel(recommendVo);
            Result.put("code",0);
            Result.put("message","查询成功");
            Result.put("data", labels);
        }catch (Exception e){
            Result.put("code",-1);
            Result.put("message","查询失败"+e.getMessage());
        }
        return Result;
    }

    @PostMapping("createLabel")
    @ApiOperation("创建标签")
    public Map<String, Object> createLabel(@RequestBody Recommend recommend) {
        Map<String, Object> Result = new HashMap<>();
        if (recommend.getLabel_name() == null || recommend.getLabel_no() == null) {
            Result.put("code",-1);
            Result.put("message","参数缺失");
            return Result;
        }
        try {
            recommendService.createLabel(recommend);
            Result.put("code", 0);
            Result.put("message", "创建成功");
            Result.put("data", recommend);
        } catch (Exception e) {
            Result.put("code", -1);
            Result.put("message", "创建失败:" + e.getMessage());
        }
        return Result;
    }

    @PostMapping("deleteLabel")
    @ApiOperation("删除标签")
    public Map<String,Object> deleteLabel(@RequestBody RecommendVo recommendVo){
        Map<String,Object> Result = new HashMap<>();
        try {
            recommendService.deleteLabel(recommendVo);
            List<Recommend> labels = recommendService.queryLabel(new RecommendVo());
            Result.put("code",0);
            Result.put("message","删除成功");
            Result.put("data", labels);
        }catch (Exception e){
            Result.put("code",-1);
            Result.put("message","删除失败，"+e.getMessage());
        }
        return Result;
    }

}